package TUILayer;

import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class MainMenu here.
 * 
 * @author (Group A) 
 * @version (2014.11.03)
 */
public class LoanUI
{
    private LoansHandler loans;
    private int choise;

    public LoanUI()
    {
        loans = new LoansHandler();
    }

    public void loanMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeLoanMenu();

            switch (choise) 
            {
                case 1: startCreateLoan();
                break;
                case 2: startFindLoan();
                break;
                case 3: startFinishLoan();
                break;
                default: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writeLoanMenu()
    {   
        try
        {
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Loan Manager ***");
            System.out.println(" (1) Create loan");
            System.out.println(" (2) Find a loan");
            System.out.println(" (3) Finish loan");
            System.out.println(" (4) Back");
            System.out.print("\n Choose an option: ");

            choise = keyboard.nextInt();
        }
        catch(Exception e) 
        {
            System.out.println("ERROR!");
            pause();
            loanMenuStart();
        }

        return choise;
    }

    private void startCreateLoan()
    {
        try 
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Fill in the information of the new loan ");
            System.out.println("Loan's ID:");
            int id = keyboard.nextInt();
            System.out.println("Borrow Date:");
            String borrowDate = keyboard.next();
            System.out.println("Period (days):");
            String period = keyboard.next();
            System.out.println("Person ID:");
            int personID = keyboard.nextInt();
            System.out.println("Enter movie title:");
            String title = keyboard.next();
            System.out.println("Enter copy serial number");
            String serialNumber = keyboard.next();

            PersonHandler people = new PersonHandler();
            DVDHandler dvds = new DVDHandler();
            DVD dvd = dvds.findDVDByName(title);
            ArrayList<Copy> copies = dvd.getCopyList();
            Copy copy = null;

            for(int i=0; i<copies.size(); i++)
            {
                if(copies.get(i).getSerialNumber().equals(serialNumber))
                {
                    copy = copies.get(i);
                }
            }
            Loan loan = new Loan(id, borrowDate, period, people.findPerson(personID), copy);
            if(loans.createLoan(loan))
            {
                if(copy.getAvailable() == true) 
                {
                    loans.createLoan(loan);
                    copy.setAvailable(false);
                    System.out.println("The loan has been created!");    
                } 
                else
                {
                    System.out.println("The copy is not available!");
                    System.out.println("The loan hasn't been created!");
                }
            }
            else
            {
                System.out.println("A loan with the same ID already exists!");
            }
        } 
        catch (Exception e)
        {
            System.out.println("Error ocurred! The loan hasn't been created!");
        }

        pause();
    }

    private void startFindLoan()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the ID of the loan you are lookig for:");
            int id = keyboard.nextInt();
            Loan loan = loans.findLoan(id);

            System.out.println("ID: " + loan.getID());
            System.out.println("Borrow date: " + loan.getBorrowDate());
            System.out.println("Period: " + loan.getPeriod());
            if(loan.getState())
            {
                System.out.println("State: Pending");
            } 
            else 
            {
                System.out.println("State: Finished");
            }

            System.out.println("Person: " + loan.getPerson().getName());
            System.out.println("Copy: " + loan.getCopy().getSerialNumber());
        } 
        catch (Exception e)
        {
            System.out.println("ERROR!");
        }
        pause();
    }

    private void startFinishLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            System.out.println("Enter loan ID:");
            int id = keyboard.nextInt();
            Loan loan = null;

            loan = loans.findLoan(id);

            if(loan.onTime())
            {
                System.out.println("The loan has been successfully finished on time!");
            } 
            else 
            {
                System.out.println("You haven`t returned on time, you do not deserve to receive anymore!");
            }
            loan.getCopy().setAvailable(true);
            loan.setState(false);
        } 
        catch(Exception e) 
        {
            System.out.println("ERROR: The loan you are looking for can not be found!");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}